//
//  ViewController.m
//  DBLPageScrollView
//
//  Created by 宝龙董 on 2016/10/20.
//  Copyright © 2016年 dbl. All rights reserved.
//

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

#import "ViewController.h"
#import <MJRefresh.h>
@interface ViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UIScrollView * mainScrollView;
@property (nonatomic , strong) UITableView * oneTableView;
@property (nonatomic , strong) UIWebView * webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self scrollView];
    [self firstTableView];
    [self twoWebView];
}

/**
 最底层scrollView
 
 @return mainScrollView
 */
- (UIScrollView *)scrollView {
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        _mainScrollView.scrollEnabled = NO;
        _mainScrollView.contentSize = CGSizeMake(kWidth, 2 * kHeight);
        _mainScrollView.backgroundColor = [UIColor greenColor];
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.bounces = NO;
        [self.view addSubview:_mainScrollView];
    }
    return _mainScrollView;
}

/**
 第一页的tableView
 
 @return oneTableView
 */
- (UITableView *)firstTableView {
    if (!_oneTableView) {
        _oneTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        _oneTableView.delegate = self;
        _oneTableView.dataSource = self;
        _oneTableView.rowHeight = 80;
        _oneTableView.tableFooterView = [[UIView alloc] init];
        [_mainScrollView addSubview:_oneTableView];
        
        _oneTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            
            [_oneTableView.mj_footer endRefreshing];
            
        }];
    }
    return _oneTableView;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    if (scrollView == _oneTableView) {
        if (scrollView.contentOffset.y > _oneTableView.contentSize.height - CGRectGetMaxY(_oneTableView.frame)+50) {
            
            [_mainScrollView setContentOffset:CGPointMake(0, kHeight) animated:YES];
        }
    }
}

/**
 第二页的webView
 
 @return webView
 */
- (UIWebView *)twoWebView {
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, kHeight, kWidth, kHeight)];
        _webView.backgroundColor = [UIColor whiteColor];
        NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
        [_webView loadRequest:[NSURLRequest requestWithURL:url]];
        [_mainScrollView addSubview:_webView];
        
        MJRefreshGifHeader * header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadMoreMessage)];
        header.lastUpdatedTimeLabel.hidden = YES;
        [header setTitle:@"下拉，返回商品详情" forState:1];
        [header setTitle:@"释放，返回商品详情" forState:2];
        _webView.scrollView.mj_header = header;
    }
    return _webView;
}

- (void)loadMoreMessage{
    
    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];

    } completion:^(BOOL finished) {
        [_webView.scrollView.mj_header endRefreshing];

    }];
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Cell = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
    
    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cell];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}
@end
